/**
 * \file
 *
 * \brief Bootloader configuration.
 *
 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip
 * software and any derivatives exclusively with Microchip products.
 * It is your responsibility to comply with third party license terms applicable
 * to your use of third party software (including open source software) that
 * may accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
 */

#ifndef CONF_BOOTLOADER_H_INCLUDED
#define CONF_BOOTLOADER_H_INCLUDED

/*
 * Debug config
 * -> Debug console
 * -> Debug LED
 */
#define DEBUG_USE_UART                                    /**< Define to Use UART (optional) */
#define DEBUG_UART_NAME            UART                   /**< USART */
#define DEBUG_UART_NB              0                      /**< USART number (Nothing or 0, 1...) */
#define DEBUG_UART_BAUDRATE        115200                 /**< USART baudrate */

#define DEBUG_UART_PIN_RX          PIO_PA8_IDX            /**< UART RX pin IO index */
#define DEBUG_UART_PIN_RX_MUX      IOPORT_MODE_MUX_A      /**< UART RX periph Mux */
#define DEBUG_UART_PIN_TX          PIO_PA9_IDX            /**< UART TX pin IO index */
#define DEBUG_UART_PIN_TX_MUX      IOPORT_MODE_MUX_A      /**< UART TX periph Mux */

#define DEBUG_LED_PIN              PIO_PA0_IDX            /**< LED pin IO index */
#define DEBUG_LED_PIN_ON_LEVEL     IOPORT_PIN_LEVEL_LOW   /**< Output level to make LED on */

/*
 * Media config
 * -> 
 * -> 
 */
#define MEDIA_USE_USART                                   /**< Define to Use USART (optional) */
#define MEDIA_USART_NAME           USART0                 /**< USART */
#define MEDIA_USART_BAUDRATE       115200                 /**< USART baudrate */

#define MEDIA_USART_PIN_RX         PIO_PA10_IDX           /**< USART RX pin IO index */
#define MEDIA_USART_PIN_RX_MUX     IOPORT_MODE_MUX_A      /**< USART RX periph Mux */
#define MEDIA_USART_PIN_TX         PIO_PA11_IDX           /**< USART TX pin IO index */
#define MEDIA_USART_PIN_TX_MUX     IOPORT_MODE_MUX_A      /**< USART TX periph Mux */
/**
 * < Define to use DBG console to modify boot
 * information (Uses OLED1 BUTTON1).
 */
//#define DBG_USE_INFO_EDIT       
//#  define DBG_INFO_EDIT_TRIGGER_PIN        (PIO_PB2_IDX)
//#  define DBG_INFO_EDIT_TRIGGER_PIN_ACTIVE (0)

/*
 * Trigger config
 * -> Buttons
 * -> SW flag
 */
//#define TRIGGER_USE_BUTTONS     /**< Define to use button trigger */
//#  define TRIGGER_LOAD_BUTTON        (PIO_PA30_IDX)
//#  define TRIGGER_LOAD_BUTTON_ACTIVE (0)
//#  define TRIGGER_SWITCH_BUTTON /* reserved for dual bank with more btn */
//#  define TRIGGER_SWITCH_BUTTON_ACTIVE (0)

//#define TRIGGER_USE_FLAG        /**< Define to use SW flag in INFO area */

/*
 * Memory config
 */
//#define MEM_LOCK_BOOT_REGION    /**< Define to lock boot region (optional) */

#define MEM_USE_FLASH           /**< Define to Use internal flash (must) */

/* Memory mapping
=====================================================================+
|<   128K   >|<                                       >|<    4 K    >|
+============+=========================================+=============+
| Bootloader | Application code                        | Information |
+=====================================================================
*/

#if 0 //def MEM_USE_FLASH

/** memory blank value */
#define MEM_BLANK_VALUE   0xFF

/** memory block size */
#define MEM_BLOCK_SIZE    (IFLASH_PAGE_SIZE)

/** memory erase block size (4K) */
#define MEM_ERASE_SIZE    (IFLASH_PAGE_SIZE * 8)

/* memory aligned (8 pages to erase) */
#define mem_align(addr)   (((addr) % MEM_ERASE_SIZE) ? ((addr) - ((addr) % MEM_ERASE_SIZE)) : (addr))

/** boot region size */
#define BOOT_SIZE          (128*1024)

/** application region size */
#define APP_SIZE           mem_align((IFLASH_SIZE - BOOT_SIZE) / 1)

/* memory address */
#define MEM0_ADDR          IFLASH_ADDR
/* MEM1_ADDR   is reserved for dual flash */

/* Boot region (single) */
#define BOOT_CODE_SIZE      (BOOT_SIZE - MEM_BLOCK_SIZE) /* reserve last page */
#define BOOT0_START         mem_align(MEM0_ADDR)
#define BOOT0_END           (BOOT0_START + BOOT_SIZE - 1)
/* BOOT1_START is reserved for dual flash  */
/* BOOT1_END   is reserved for dual flash  */

/* Application region (single) */
#define APP_CODE_SIZE       (APP_SIZE - MEM_ERASE_SIZE) /* reserve last pages */
#define APP0_START          mem_align(MEM0_ADDR + BOOT_SIZE)
#define APP0_END            (APP0_START + APP_CODE_SIZE - 1)

/* boot: boot region number, always 0 */
#define APP_START(boot)     (APP0_START) /* Always boot from 1st region */

/* Information page (follow app buffer) */
#define INFO_SIZE           (MEM_ERASE_SIZE) /**< Boot information size */
#define INFO0_ADDR          (APP0_END + 1)
/* INFO1_ADDR   is reserved for dual flash */

/* for_boot: purpose, true for boot, false for buffer */
#define INFO_ADDR(for_boot) (INFO0_ADDR) /* Info always save to last flash page */

#endif /* #ifdef MEM_USE_FLASH */


//------------------------------------------------------------------------------
// Modes
//------------------------------------------------------------------------------

/**
 * Communication protocol
 *  -> Xon/Xoff (USE_XON_XOFF)
 */
//#define USE_XON_XOFF

/**
 * Media layer
 *  -> Usart #0 (USE_US0)
 */
//#define USE_US0
//    #define US0_BAUDRATE   115200

/**
 * Debug
 *  -> Dbgu (USE_DBGU)
 */
//#define USE_DBGU
//    #define DBGU_BAUDRATE  115200

/**
 * Memory type
 *  -> Flash (USE_FLASH)
 */
//#define USE_FLASH

/**
 * Trigger condition
 *  -> Dummy/Always load (USE_DUMMY)
 *  -> Hardware switches (USE_SWITCHES)
 */
//#define USE_DUMMY
//#define USE_SWITCHES

/**
 * Timing measurement
 *  -> Timer #0 (USE_TIMER0)
 */
//#define USE_TIMER0

//------------------------------------------------------------------------------
// Options
//------------------------------------------------------------------------------

/**
 * Name:    USE_BOOT_REGION_LOCKING
 * Effect:  Locks the boot region, denying write access on it.
 *          The boot segment must occupate a whole region, or it will
 *          not be possible to correctly program the application.
 *           => BOOTLOADEREND multiple of 0x4000
 */
#define USE_BOOT_REGION_LOCKING

/**
 * Name:    USE_ENCRYPTION
 * Effect:  The firmware must be transmitted in an encrypted form when this
 *          option is selected.
 * Parameters:
 *  - Algorithm used for encryption
 *      -> AES (ENCRYPTION_AES_LTC, ENCRYPTION_AES_REF)
 *  - Mode of encryption
 *      -> CTR (ENCRYPTION_CTR)
 *  - Length of key in bytes (ENCRYPTION_KEY_LENGTH)
 *  - Key, string representation of its hexadecimal value (ENCRYPTION_KEY)
 *  - Length of block in bytes (ENCRYPTION_BLOCK_LENGTH)
 *  - Initialization vector (ENCRYPTION_IV)
 */
/*#define USE_ENCRYPTION
    #define ENCRYPTION_AES_HARD
    #define ENCRYPTION_ECB
    #define ENCRYPTION_KEY_LENGTH    16
    #define ENCRYPTION_KEY           "C0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE"
    #define ENCRYPTION_BLOCK_LENGTH  16
    #define ENCRYPTION_IV            "C0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE"*/

/**
 * Name:    USE_MEMORY_PARTIONING
 * Effect:  When active, the application space in flash is divided into two
 *          regions. One act as a buffer to receive a firmware upgrade, while
 *          the other contains the code which will be executed. This makes it
 *          possible to have, at all time, at least one working firmware in the
 *          device (even after an upgrade failure).
 * Parameters:
 *          Size of one region (REGION_SIZE)
 *            -> Note that REGION_SIZE should be equal to
 *               (flash size - bootloader size - page size) / 2
 *            -> One page is reserved to store information about regions
 *               -> Signatures, length of firmware, etc.
 *            -> Ex with SAM7XC256, BOOTSIZE = 0x8000
 *               -> (0x40000 - 0x8000 - 0x100) / 2 = 0x1BF00
 *            -> Regions must be aligned on page and multiple of MEMORY_PAGE_SIZE
 */
/*#define USE_MEMORY_PARTITIONING
    #define REGION_SIZE     0x1BF00*/

#endif /* CONF_BOOTLOADER_H_INCLUDED */

